#!/usr/bin/env python
# encoding: utf-8


"""
@file: diandeweizhi.py
@time: 2016/11/22 上午9:28
"""
# 点的位置关系
from mathsolver.functions.base import *
from mathsolver.functions.budengshi import common_opers as co
from sympy import S


# 25.点在线两侧，点在线同侧问题
# 点A(3,1)和B(-4,6)在直线3x-2y+a=0的两侧,则a的取值范围是().
class DianDeWeiZhi001(BaseFunction):
    def solver(self, *args):
        self.label.add('点在线的两侧或同侧')
        p1_value = args[0].sympify()
        p2_value = args[1].sympify()
        line_eq = args[2]
        text = '同侧' if args[3].find('同侧') >= 0 else '两侧'
        line_l, line_r = line_eq.sympify()
        line_f = line_l - line_r
        x_symbol, y_symbol = sympify('x'), sympify('y')
        self.steps.append(['\because 点%s和%s在直线%s的%s' % (str(p1_value), str(p2_value), line_eq.printing(), text), ''])
        f1 = line_f.subs(zip((x_symbol, y_symbol), p1_value))
        f2 = line_f.subs(zip((x_symbol, y_symbol), p2_value))
        f = f1 * f2
        if text == '同侧':
            ineq = BaseIneq([f, '>', '0'])
            ineq_str = str(f) + '>0'
        else:
            ineq = BaseIneq([f, '<', '0'])
            ineq_str = str(f) + '<0'
        self.steps.append(['\therefore %s' % ineq.printing(), ''])
        r_set = solveset(ineq_str, domain=S.Reals)
        self.steps.append(['解的取值范围为: ', new_latex(r_set)])
        self.output.append(co.to_base_interval(r_set))
        return self


class DianDeWeiZhi002(BaseFunction):
    def solver(self, *args):
        raise Exception('try error')
        return self


class DianDeWeiZhi(BaseFunction):
    CLS = [DianDeWeiZhi001, DianDeWeiZhi002]

    def solver(self, *args):
        r = None
        for cl in DianDeWeiZhi.CLS:
            try:
                r = cl(verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
